import { Body, Controller, Post, UseFilters } from '@nestjs/common';
import { Public } from './public.decorator';
import { AuthService } from './auth.service';
import { HttpExceptionFilter } from '../../exception/http-exception.filter';
import { error, success } from 'src/utils';

@Controller('auth')
export class AuthController {

  constructor(private readonly authService: AuthService) {}
  // login接口添加了 Public 装饰器，表示该接口不需要进行身份验证
  @Public()
  @Post('login')
  @UseFilters(new HttpExceptionFilter())
  async login(@Body() params: { username: string, password: string }): Promise<{code: number, data?: { token: string}, message: string }> {
    const { username, password } = params
    return this.authService.login(username, password)
      .then(data => success(data, '登录成功'))
      .catch(err => error('登录失败'))
  }
}
